import json
import time
from hyper.contrib import HTTP20Adapter
import httpx

import requests
requests.packages.urllib3.disable_warnings()

# 获取签到状态
def openBox():
    currentTime = str(int(time.time()))
    host = 'https://wuxia8.weimigames.com'
    path = '/equip/openBox2?uuid='+uuid+'&token='+token+'&version='+version+'&time=' + currentTime
    url = host + path
    headers['path'] = path
    data = {}
    data = baseRequest(url, headers, data)
    # data = {"type":1,"actEquip":{"a":{"chuan":{"1":{"equipId":"10106","level":100,"eps":{"hp_max":11300,"atk":2650,"def":900,"speed":450,"jiyun":600,"xixue":250},"hhList":{"10101":0,"10102":0,"10103":0,"10104":0,"10105":0,"10106":0,"10107":0,"10108":0,"10109":0,"50002":0},"mrhh":"50002","hh":"10107"},"2":{"equipId":"10206","level":76,"eps":{"hp_max":7965,"atk":2045,"def":687,"speed":333,"xixue":351,"baonue":193},"hhList":{"10201":0,"10202":0,"10203":0,"10204":0,"10205":0,"10206":0,"10207":0,"10208":0,"10209":0,"50001":0},"mrhh":"10208","hh":"10207"},"3":{"equipId":"10306","level":78,"eps":{"hp_max":8223,"atk":2076,"def":706,"speed":344,"fanji":291,"hsbaoji":105},"hhList":{"10301":0,"10302":0,"10303":0,"10304":0,"10305":0,"10306":0,"10307":0,"10308":0,"10309":0,"50000":0},"mrhh":"10309","hh":"50000"},"4":{"equipId":"10406","level":76,"eps":{"hp_max":7850,"atk":2012,"def":676,"speed":325,"shanbi":346,"hslianji":91},"hhList":{"10401":1,"10402":1,"10403":1,"10404":1,"10405":1,"10406":1,"10407":1,"10408":1,"10409":1},"mrhh":"10407","hh":""},"5":{"equipId":"10507","level":72,"eps":{"hp_max":7887,"atk":2009,"def":680,"speed":338,"fanji":479,"renai":320},"hhList":{"10501":1,"10502":1,"10503":1,"10504":1,"10505":1,"10506":1,"10507":1,"10508":1,"10509":1},"mrhh":"10508","hh":""},"6":{"equipId":"10607","level":67,"eps":{"hp_max":7256,"atk":1885,"def":626,"speed":312,"baoji":478,"baonue":347},"hhList":{"10601":1,"10602":1,"10603":1,"10604":1,"10605":1,"10606":1,"10607":1,"10608":1,"10609":1},"mrhh":"10607","hh":""},"7":{"equipId":"10706","level":78,"eps":{"hp_max":8227,"atk":2081,"def":703,"speed":341,"jiyun":349,"hsjiyun":97},"hhList":{"10701":1,"10702":1,"10703":1,"10704":1,"10705":1,"10706":1,"10707":1,"10708":1,"10709":1},"mrhh":"10702","hh":""},"8":{"equipId":"10807","level":67,"eps":{"hp_max":7257,"atk":1892,"def":630,"speed":308,"xixue":471,"hsshanbi":148},"hhList":{"10801":1,"10802":1,"10803":1,"10804":1,"10805":1,"10806":1,"10807":1,"10808":1,"10809":1},"mrhh":"10807","hh":""},"9":{"equipId":"10907","level":75,"eps":{"hp_max":8318,"atk":2101,"def":714,"speed":359,"jiyun":442,"hsjiyun":153},"hhList":{"10901":1,"10902":1,"10903":1,"10904":1,"10905":1,"10906":1,"10907":1,"10908":1,"10909":1},"mrhh":"10909","hh":""},"10":{"equipId":"11007","level":69,"eps":{"hp_max":7496,"atk":1944,"def":649,"speed":321,"jiyun":385,"baonue":349},"hhList":{"11001":1,"11002":1,"11003":1,"11004":1,"11005":1,"11006":1,"11007":1,"11008":1,"11009":1},"mrhh":"11005","hh":""},"11":{"equipId":"11106","level":75,"eps":{"hp_max":7797,"atk":2017,"def":676,"speed":325,"lianji":269,"hsfanji":103},"hhList":{"11101":1,"11102":1,"11103":1,"11104":1,"11105":1,"11106":1,"11107":1,"11108":1,"11109":1,"11115":1},"mrhh":"11109","hh":""},"12":{"equipId":"11206","level":79,"eps":{"hp_max":7967,"atk":2035,"def":682,"speed":329,"shanbi":304,"hsbaoji":89},"hhList":{"11201":1,"11202":1,"11203":1,"11204":1,"11205":1,"11206":1,"11207":1,"11208":1,"11209":1},"mrhh":"11204","hh":""}},"jlRank":1,"jlLevel":6,"jlpos":0,"jlGm":0,"jlAll":1,"box":161,"openList":{"3":{"equipId":"10102","mrhh":"10104","hh":"","level":78,"eps":{"hp_max":6557,"atk":1811,"def":585,"speed":221,"xixue":93}}},"count":10767,"time":1701310128,"jjc":1,"dayFjCount":0,"trader":1000,"czpf":{"10101":58,"10102":88,"10103":109,"10104":126,"10105":141,"10106":128,"10107":128,"10108":89,"10109":29,"10201":43,"10202":90,"10203":135,"10204":146,"10205":146,"10206":129,"10207":116,"10208":75,"10209":33,"10301":49,"10302":87,"10303":114,"10304":167,"10305":158,"10306":133,"10307":107,"10308":73,"10309":33,"10401":40,"10402":91,"10403":132,"10404":138,"10405":146,"10406":140,"10407":118,"10408":80,"10409":25,"10501":60,"10502":93,"10503":117,"10504":141,"10505":142,"10506":146,"10507":103,"10508":70,"10509":27,"10601":40,"10602":101,"10603":121,"10604":140,"10605":135,"10606":124,"10607":116,"10608":76,"10609":29,"10701":30,"10702":92,"10703":106,"10704":126,"10705":142,"10706":126,"10707":126,"10708":94,"10709":39,"10801":52,"10802":92,"10803":124,"10804":120,"10805":164,"10806":125,"10807":104,"10808":75,"10809":21,"10901":48,"10902":97,"10903":127,"10904":135,"10905":189,"10906":129,"10907":113,"10908":81,"10909":28,"11001":55,"11002":73,"11003":118,"11004":149,"11005":148,"11006":154,"11007":109,"11008":61,"11009":21,"11101":41,"11102":98,"11103":108,"11104":121,"11105":139,"11106":141,"11107":113,"11108":67,"11109":32,"11201":40,"11202":86,"11203":122,"11204":125,"11205":142,"11206":148,"11207":120,"11208":81,"11209":29}}},"time":1701314752}
    if data['type'] == 0:
        print(data['win']['msg'])
        return
    actEquip = data['actEquip']
    if 'a' in actEquip:
        u = actEquip['a']
    else:
        u = actEquip['u']

    openList = u['openList'] # 当前开出来的
    chuan = u['chuan'] # 身上穿的
    print('身上穿着\r')
    chuanDiff = {}
    chuanStr = {}
    # 当前穿的
    for item in chuan:
        item = chuan[item]
        level = item['level']
        prefix = item['equipId'][0:3]
        equipId = equipIds[prefix]
        eps = item['eps']
        if prefix is not chuanDiff:
            chuanDiff[prefix] = {}
        string = ''
        for key in eps:
            string += attrKeys[key] + '：' + str(eps[key]) + '、'
            chuanDiff[prefix][key] = str(eps[key])
        chuanStr[prefix] = f'穿着位置：{equipId}，等级Lv：{level}，{string}\r'
        print(chuanStr[prefix])
    print('\r')
    print('当前开出来的是\r')
    for open in openList:
        id = open
        open = openList[open]
        level = open['level']
        prefix = open['equipId'][0:3]
        equipId = equipIds[prefix]
        eps = open['eps']
        string = ''
        diff = chuanDiff[prefix]
        diffCount = 0
        for key in eps:
            diffKey = ''
            val = str(eps[key])
            if key in diff:
                if eps[key] >= int(diff[key]):
                    diffKey = '▲'
                    diffCount += 1
                else:
                    diffKey = '▼'
            string += attrKeys[key] + '：' + val + '('+diffKey+')、'
        print("当前" + chuanStr[prefix])
        print(f'ID：{id}，获取：{equipId}，等级Lv：{level}，{string}\r')
        if diffCount >= 3:
            sellerIn(id)
        else:
            sellerOut(id)

# 出售
def sellerOut(openId):
    currentTime = str(int(time.time()))
    host = 'https://wuxia8.weimigames.com'
    path = '/equip/deal2?uuid='+uuid+'&token='+token+'&version='+version+'&time=' + currentTime
    url = host + path
    headers['path'] = path
    openId = str(openId)
    data = {"openId": openId, "type": 1}
    data = baseRequest(url, headers, data)
    if 'userInfo' in data:
        power = str(data['userInfo']['power'])
        print('出售成功 openId=' + openId + '，最新战力为：' + power)
    else:
        print('出售失败 openId=' + openId)


# 穿戴装备
def sellerIn(openId):
    currentTime = str(int(time.time()))
    host = 'https://wuxia8.weimigames.com'
    path = '/equip/tihuan2?uuid='+uuid+'&token='+token+'&version='+version+'&time=' + currentTime
    url = host + path
    headers['path'] = path
    openId = str(openId)
    data = {"openId": openId, "type": 0}
    data = baseRequest(url, headers, data)
    if 'userInfo' in data:
        power = str(data['userInfo']['power'])
        print('穿戴成功 openId=' + openId + '，最新战力为：' + power)
    else:
        print('穿戴失败 openId=' + openId)

# 发起请求
def baseRequest(url, headers , data):
    data = json.dumps(data)
    # print(f'请求地址{url},请求参数{data}')
    client = httpx.Client(http2=True, verify=False)
    result = client.post(url, headers=headers, data=data)
    content = result.content.decode('utf8')
    data = json.loads(content)
    return data

if __name__ == '__main__':

    headers = {
        'method': 'POST',
        'scheme': 'https',
        'authority': 'wuxia8.weimigames.com',
        'content-type': 'application/json',
        'accept': '*/*',
        'sec-fetch-site': 'same-site',
        'accept-language': 'zh-CN,zh-Hans;q=0.9',
        'accept-encoding': 'gzip, deflate, br',
        'sec-fetch-mode': 'cors',
        'origin': 'https://sskill.weimigames.com',
        'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 16_5_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148',
        'referer': 'https://sskill.weimigames.com/',
        # 'content-length': '2',
        'sec-fetch-dest': 'empty',
        'Connection': 'Keep-Alive'
    }
    uuid = '167636'
    token = 'fd65857ab6c5a1b6d466b20a7affffc8'
    version = '1.1.1'
    attrKeys = {
        'hp_max': '生命',
        'atk': '攻击',
        'def': '防御',
        'speed': '速度',
        'jiyun': '击晕',
        'baoji': '暴击',
        'xixue': '吸血',
        'lianji': '连击',
        'baonue': '暴伤增加',
        'fanji': '反击',
        'hsbaoji': '暴击抵抗',
        'shanbi': '闪避',
        'hslianji': '连击抵抗',
        'renai': '暴伤减免',
        'hsjiyun': '眩晕抵抗',
        'hsshanbi': '闪避抵抗',
        'hsfanji': '反击抵抗',
        'hsxixue': '反击吸血'
    }
    equipIds = {
        '101': '头部',
        '102': '衣服',
        '103': '武器',
        '104': '玉佩',
        '105': '肩部',
        '106': '项链',
        '107': '护腕',
        '108': '手镯',
        '109': '腰带',
        '110': '裤子',
        '111': '戒指',
        '112': '鞋子',
    }
    print('-----------------游戏-开始-----------------')
    for i in range(10):
        openBox()
        time.sleep(1)